package org.stragma.apply.util

import aterm._
import aterm.pure.SingletonFactory
import java.io.FileInputStream
import java.io.FileOutputStream

import org.stragma.apply.signatures.Apply._

import org.stragma.util._
import org.stragma.util.Diff._

trait TermInputOutput {
	
	/*
	 * Loads a AST into memory and converts it into
	 * TModule for use by the re-writers
	 */
	def loadTermFromFile(file : String) : TModule = {
        val is = new FileInputStream(file)
        val f = SingletonFactory.getInstance ()
        val iterm = f.readFromTextFile(is)
        is.close ()
        
        // Convert that aterm into a Scala structure
      	ATermToTModule (iterm)
	}
	
	/*
	 * Saves the AST to file, but before doing so it converts
	 * the Scala structure back to ATerms
	 */
	def saveTermToFile(file : String, ast : TModule) = {
         val oterm = TModuleToATerm(ast)
        
        // Write out the converted aterm
        val os = new FileOutputStream(file)
        oterm.writeToTextFile (os)
        os.write(10)
        os.close()
	}
	
	/*
	 * Compares two files for differences
	 */
	def validateResults(diffList : Traversable[DiffResult]) =
		diffList.foreach { 
			case e : Equal =>
			case _ => assert(false)
		}
}